<?

//Data loading
//$title='KAS - BON';
//$title2='BON SEMENTARA';
$title='JOB ORDER';
$title2='PREPARATION ND';
//if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
//else $perush = 'PT. MASAJI PRAYASA CARGO';

$perush = 'PT. AMPEL JAYA';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("L","mm","Letter");
$pdf->AddPage();
$Result = GridList ();
$pdf->GridTable($Result);
$pdf->Output();


function GridList () {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            define("PRIMARY_KEY","jo_id");
                    //$SQL = "SELECT * FROM ais_advance WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

                    /* DEFAULT
                    $SQL = "SELECT * , b.curr as job_costing_curr FROM ais_advance a
                            JOIN tbljoborder_costing b ON a.jo_costing_id = b.jo_costing_id
                            JOIN tblactivity_nr c ON b.activities_id = c.act_nr_id
                            JOIN tbljoborder d ON b.jo_id = d.jo_id
                            JOIN tblproject e ON d.project_id = e.project_id
                            JOIN tbldebtur g ON d.debtur = g.CUCODE
                            WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";
                            //echo"$SQL"; exit;
                    return $DBConnection->dbc->get_results($SQL,ARRAY_A);
                    */


                    /*$SQLProject = "SELECT *, c.nama as staff_marketing, e.nama as staff_operasional,
                                    if(a.jo_appv_status = 'draft', 'Draft', if(a.jo_appv_status = 'wait_approved', 'Wait for Approval', if(a.jo_appv_status = 'approved', 'Approved', 'No Approval Status'))) as approval_status,
                                    if(a.jo_costing_appv_status = 'draft', 'Draft', if(a.jo_costing_appv_status = 'wait_approved', 'Wait for Approval', if(a.jo_costing_appv_status = 'approved_1', 'Approved 1', if(a.jo_costing_appv_status = 'approved_2', 'Approved 2', 'No Approval Status')))) as approval_costing_status
                                    FROM `$TableName` as a
                                    JOIN tblproject as b ON a.project_id = b.project_id
                                    JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id
                                    JOIN tbldebtur as d ON a.debtur = d.CUCODE
                                    LEFT JOIN ".$DBGeneral.".tblstaff as e ON a.staff_jo_costing_id = e.staff_id
                                    WHERE a.".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."' ORDER BY a.".PRIMARY_KEY." ASC";

                    $ResultProject  = $DBConnection->dbc->get_results($SQLProject,ARRAY_A);*/

                    $SQLGrid = "SELECT * FROM tbljoactivity as a
                                JOIN tblservices as b on a.services_id = b.services_id
                                WHERE ".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."' AND a.bill_nd = 'set' ORDER BY a.act_id";

                    //$Result  = $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);
                    return $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);
}

function ReadHeader($nobkt) {
  global $DBConnection;
  $SQL = "SELECT *,DATE_FORMAT(tglbkt,'%d %M, %Y') as spell_date  FROM ais_advance WHERE nobkt='$nobkt' LIMIT 1";
  return $DBConnection->dbc->get_row($SQL);
}


class PDFReport extends PDF {

    function GetHeader() {
        global $title,$title2,$perush,$page,$DEFINE_FONT, $DBConnection, $DBGeneral;
        define("PRIMARY_KEY","jo_id");

        $SQLHeader = "SELECT *, c.nama as staff_marketing, e.nama as staff_operasional,
                    if(a.jo_appv_status = 'draft', 'Draft', if(a.jo_appv_status = 'wait_approved', 'Wait for Approval', if(a.jo_appv_status = 'approved', 'Approved', 'No Approval Status'))) as approval_status
                    FROM tbljoborder as a
                    JOIN tblproject as b ON a.project_id = b.project_id
                    JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id
                    JOIN tbldebtur as d ON a.debtur = d.CUCODE
                    LEFT JOIN ".$DBGeneral.".tblstaff as e ON a.staff_jo_costing_id = e.staff_id
                    WHERE a.".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."'  ORDER BY a.".PRIMARY_KEY." ASC";
//echo "$SQLHeader"; exit;
            //$ResultHeader  = $DBConnection->dbc->get_results($SQLHeader,ARRAY_A);
            return $DBConnection->dbc->get_results($SQLHeader,ARRAY_A);
    }

    function Header() {
        global $title,$title2,$perush,$page,$DEFINE_FONT, $DBConnection, $DBGeneral;
        define("PRIMARY_KEY","jo_id");
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }
        $Field = ReadHeader(GetParam("nobkt",""));
                $this->spell_date = strtoupper($Field->spell_date);
                list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

        $this->SetFont('Arial','',10);
                $this->Cell(50,5,$perush,0,1,'C',0);
                $this->SetFont('Arial','',16);
                $this->SetLeftMargin(20);
                $this->Cell(240,7,$title,0,1,'C',0);
                $this->SetLeftMargin(20);
                $this->Cell(240,7,$title2,0,1,'C',0);
        $this->SetFont('Arial','',10);
                $this->SetLeftMargin(20);
        $this->Ln();
                $this->SetLeftMargin(20);
                
            $ResultHeader = $this->GetHeader();

            //echo "<pre>"; print_r($ResultHeader); exit;

            $SQLCurr = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
                        WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

            $Curr = $DBConnection->dbc->get_var($SQLCurr);


            $w=array(10,15,140,50,25,30);
            $this->SetFont('Arial','',8);
            //$this->Cell(155,7,"NO JO: ".GetParam("nobkt",""),"LBT",0,'L',0);
            //$this->Cell(85,7,"TANGGAL: ".$this->day."/".$this->month."/".$this->year,"RBT",0,'L',0);
            $this->Cell(20,7,"NO JO",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(85,7,$ResultHeader[0]['jo_code'],0,0,'L',0);
            $this->Ln();
            //$this->Cell(85,7,"TANGGAL: ".$this->day."/".$this->month."/".$this->year,0,0,'L',0);
            $this->Cell(20,7,"TANGGAL",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(85,7,$ResultHeader[0]['jo_date'],0,0,'L',0);
            $this->Ln();
            $this->Cell(20,7,"CUSTOMER",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(210,7,$ResultHeader[0]['CUNAME'],0,0,'L',0);
            $this->Ln();
            $this->Cell(20,7,"PROJECT",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(240,7,$ResultHeader[0]['project_name'],0,0,'L',0);
            $this->Ln();
            $this->Cell(20,7,"VESSEL",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(240,7,$ResultHeader[0]['vessel'],0,0,'L',0);
            $this->Ln();
            $this->Cell(20,7,"BL NO ",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(240,7,$ResultHeader[0]['bl_no'],0,0,'L',0);
            $this->Ln();
            $this->Cell(20,7,"INVOICE",0,0,'L',0);
            $this->Cell(5,7,":",0,0,'C',0);
            $this->Cell(240,7,$ResultHeader[0]['invoice_no'],0,0,'L',0);
            $this->Ln();
            $this->Ln();
            
            //$this->Cell($w[2],7,"JUMLAH ($Curr)",1,0,'C',0);
            $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths
            global $DBConnection;
    $w=array(10,15,140,50,25,30);
        //Data
        if ($MyResult){
        	$this->Cell(20,7,"Ready for Create ND",0,0,'L',0);
        	$this->Ln();
        	//$this->Cell($w[0],7,"NO.",1,0,'C',0);
        	$this->Cell($w[0],7,"NO.",1,0,'C',0);
            //$this->Cell($w[1],7,"URAIAN",1,0,'C',0);
            $this->Cell($w[3],7,"SERVICES NAME",1,0,'C',0);
            $this->Cell($w[0],7,"QTY",1,0,'C',0);
            $this->Cell($w[1],7,"UNIT",1,0,'C',0);
            $this->Cell($w[1],7,"CURR",1,0,'C',0);
            $this->Cell($w[1],7,"RATE",1,0,'C',0);
            $this->Cell($w[4],7,"TAX RATE",1,0,'C',0);
            $this->Cell($w[4],7,"TAX AMOUNT",1,0,'C',0);
            $this->Cell($w[4],7,"TAX AMOUNT (USD)",1,0,'C',0);
            $this->Cell($w[4],7,"TOTAL AMOUNT",1,0,'C',0);
            $this->Cell($w[4],7,"TOTAL AMOUNT (USD)",1,0,'C',0);
            $this->Ln();
        foreach($MyResult as $row)
        {

            
            $TotalTax = $TotalTax + $row["tax_amount"];
            $TotalTaxUsd = $TotalTaxUsd + $row["tax_amount_usd"];

            if($row["curr"]=="USD"){
                $TotalAmountUsd = $TotalAmountUsd + $row["total_amount_usd"];
            } else {
                $TotalAmount = $TotalAmount + $row["total_amount"];
            }

            $this->Cell($w[0],7,$row["urut"].".",'LR',0,'R');
            $this->Cell($w[3],7,$row["services_name"],'LR',0,'L');
            $this->Cell($w[0],7,$row["qty"],'LR',0,'L');
            $this->Cell($w[1],7,$row["unit"],'LR',0,'L');
            $this->Cell($w[1],7,$row["curr"],'LR',0,'L');
            $this->Cell($w[1],7,FormatNumber($row["rate"]),'LR',0,'R');

            $taxdesc = explode("-", $row["tax_rate"]);
            $taxamt = $taxdesc[0] * 100;

            $this->Cell($w[4],7,$taxamt."% ".$taxdesc[1],'LR',0,'L');
            $this->Cell($w[4],7,FormatNumber($row["tax_amount"]),'LR',0,'R');
            $this->Cell($w[4],7,FormatNumber($row["tax_amount_usd"]),'LR',0,'R');
            
            if($row["curr"]=="IDR"){
                $this->Cell($w[4],7,FormatNumber($row["total_amount"]),'LR',0,'R');
            } else {
                $this->Cell($w[4],7,FormatNumber(0),'LR',0,'R');
            }

            $this->Cell($w[4],7,FormatNumber($row["total_amount_usd"]),'LR',0,'R');
            
            $this->SetFont('Arial','',8);

            $this->Ln();
        }
            
                $cc = new intToRupiah;
                $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_advance WHERE ais_advance.nobkt='".GetParam("nobkt","")."' AND tblcurrency.tusymbol=ais_advance.curr AND ktrasl='".GetOfficeID()."'");
                //$DBConnection->dbc->debug();
                $cc->setTail("");
                $cc->setNumber($Total);
                $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

            
	            $this->Cell($w[0]+$w[3],7,"TOTAL:",1,0,'R',0);
	            $this->Cell(80,7,"",1,0,'R',0);
	            $this->Cell(25,7,FormatNumber($TotalTax,2),1,0,'R',0);
	            $this->Cell(25,7,FormatNumber($TotalTaxUsd,2),1,0,'R',0);
	            $this->Cell(25,7,FormatNumber($TotalAmount,2),1,0,'R',0);
	            $this->Cell(25,7,FormatNumber($TotalAmountUsd,2),1,0,'R',0);
	            $this->Ln();
                $this->Ln();
			
                $GetPerson = $DBConnection->dbc->get_row("SELECT * FROM tblprofile", ARRAY_A);
                //return $DBConnection->dbc->get_row($SQL);

                $ResultFooter = $this->GetHeader();
                $Debtur = trim($ResultFooter[0]["CUNAME"]);

                $this->Cell(65,7,"DISETUJUI",0,0,'C',0);
                $this->Cell(65,7,"MENGETAHUI",0,0,'C',0);
                $this->Cell(65,7,"PENERIMA",0,0,'C',0);
            	$this->Ln();$this->Ln();$this->Ln();
                $this->Cell(65,7,"( ".$Debtur." )",0,0,'C',0);
                $this->Cell(65,7,"( ".$GetPerson["ttdbs2"]." )",0,0,'C',0);
                //$this->Cell(65,7,"( ".$row["debnam"]." )",0,0,'C',0);

		} else {
			$this->Cell(20,7,"No Service",0,0,'L',0);	
		}

        //Closure line
}

    function Footer() {
        //Closure line
        //$this->Cell(205,0,'','T');
    }

}


?>
